﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Admin.Common;
using Zocono.WCS.Admin.Models.User;
using Zocono.WCS.Admin.Models.WorkTaskManager;
using Zocono.WCS.Application.AppConfigureManage;
using Zocono.WCS.Application.AppTraces;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.Enums;
using Zocono.WCS.Infrastructure.ApllicationDto.WCSTaskDto;

namespace Zocono.WCS.Admin.Controllers.WorkTaskManagerMoudle
{
    [BasicAuth]
    public class HistoryWorkTaskInfoController : BaseController
    {
        private readonly HistoryWorkTaskInfoApp _workTaskInfoApp;
        private readonly TaskRouteDetailRecordInfoApp _taskRouteDetailRecordInfoApp;

        public HistoryWorkTaskInfoController(HistoryWorkTaskInfoApp workTaskInfoApp, TaskRouteDetailRecordInfoApp taskRouteDetailRecordInfoApp, IMemoryCache memoryCache, TracesApp tracesApp, IHttpContextAccessor httpContextAccessor) : base(memoryCache, tracesApp, httpContextAccessor)
        {
            this._workTaskInfoApp = workTaskInfoApp;
            this._taskRouteDetailRecordInfoApp = taskRouteDetailRecordInfoApp;
        }

        public IActionResult Index()
        {
            return View();
        }

        public ActionResult WorkTaskInfoAsync()
        {
            var taskSearchData = new WorkTaskInfoSearchData();
            //await AddTracesInfo();
            return View(taskSearchData);
        }

        public async Task<JsonResult> GetAllWorkTaskInfoAsync()
        {
            var where = new QueryHistoryWorkTaskInfoDto();
            where.PageIndex = (Start / Length) + 1;
            where.PageSize = Length;
            where.FromStation = Request.Form["FromStation"].ToString();//起 始 工 位
            where.ToStation = Request.Form["ToStation"].ToString();//目 标 工 位
            where.WmsFromStation = Request.Form["WmsFromStation"].ToString();//WMS起始工位
            where.WmsToStation = Request.Form["WmsToStation"].ToString();//WMS目标工位
            where.WmsFromPosition = Request.Form["WmsFromPosition"].ToString();//WMS起始位置
            where.WmsToPosition = Request.Form["WmsToPosition"].ToString();//WMS目标位置
            where.PalletCode = Request.Form["PalletCode"].ToString();//托 盘 编 码
            where.RouteInfo = Request.Form["RouteInfo"].ToString();//巷道


            where.CurrentDispatchType = Request.Form["CurrentDispatchType"].ToInt32();//当前调度类型
            var TaskCode = Request.Form["TaskCode"].ToInt32();//任务 编 码
            if (TaskCode != 0)
            {
                where.TaskCode = TaskCode;
            }
            var WmsTaskCode = Request.Form["WmsTaskCode"].ToInt32();//WMS任务编码
            if (WmsTaskCode != 0)
            {
                where.WmsTaskCode = WmsTaskCode;
            }
            var TaskType = Request.Form["TaskType"].ToInt32();//任 务 类 型
            if (TaskType != -1)
            {
                where.TaskType = TaskType;
            }
            var TaskState = Request.Form["TaskState"].ToInt32();//工 作 状 态
            if (TaskState != -1)
            {
                where.TaskState = TaskState;
            }
            var TaskProcessState = Request.Form["TaskProcessState"].ToInt32();//任务处理状态
            if (TaskProcessState != -1)
            {
                where.TaskProcessState = TaskProcessState;
            }

            var UploadWmsState = Request.Form["UploadWmsState"].ToInt32();
            if (UploadWmsState != -1)
            {
                where.UploadWmsState = UploadWmsState;
            }
            where.StarTime = Request.Form["startTime"];//创建时间 开始
            where.EndTime = Request.Form["endTime"];//创建时间 结束
            var workTaskInfo = await _workTaskInfoApp.GetPagedWorkTaskInfoAsync(where);
            var workTaskPageDatas = new List<WorkTaskInfoPageData>();
            if (workTaskInfo != null && workTaskInfo.Result != null && workTaskInfo.Result.Count > 0)
            {
                foreach (var item in workTaskInfo.Result)
                {
                    workTaskPageDatas.Add(new WorkTaskInfoPageData()
                    {
                        ID = item.ID,
                        RouteID = item.RouteID,
                        FromStation = item.FromStation,
                        ToStation = item.ToStation,
                        Priority = item.Priority,
                        PalletCode = item.PalletCode,
                        TaskCode = item.TaskCode,
                        TaskProcessState = (int)item.TaskProcessState,
                        TaskState = (int)item.TaskState,
                        TaskType = (int)item.TaskType,
                        SaveTime = item.SaveTime,
                        UpdateTime = item.UpdateTime,
                        WmsTaskCode = item.WmsTaskCode,
                        DependWmsTaskCode = item.DependWmsTaskCode,
                        WmsFromStation = item.WmsFromStation,
                        WmsFromPosition = item.WmsFromPosition.ToString() + "->" + item.WmsToPosition.ToString(),
                        WmsToStation = item.WmsToStation,
                        WmsToPosition = item.WmsToPosition,
                        CurrentRounteDetailID = item.CurrentRounteDetailID,
                        CurrentDetailTaskState = (int)item.CurrentDetailTaskState,
                        CurrentDispatchType = (int)item.CurrentDispatchType,
                        CurrentFromEquipment = item.CurrentFromEquipment,
                        CurrentFromCargo = item.CurrentFromCargo,
                        CurrentToEquipment = item.CurrentToEquipment,
                        CurrentToCargo = item.CurrentToCargo,
                        CurrentCreateTime = item.CurrentCreateTime,
                        CurrentUpdateTime = item.CurrentUpdateTime,
                        UploadWmsState = (int)item.UploadWmsState,
                        UploadWmsTime = item.UploadWmsTime
                    });
                }
            }
            var pageData = new TableData<WorkTaskInfoPageData>(Draw, workTaskInfo.Total, workTaskInfo.Total, workTaskPageDatas);

            //await AddTracesInfo(5, 0, "获取任务列表信息");
            return Json(pageData);
        }

        public async Task<JsonResult> BackSetTaskAsync()
        {
            long id = Request.Form["id"].ToInt64();
            var palletCode = Request.Form["palletCode"].ToString();
            var wmsTaskCode = Request.Form["wmsTaskCode"].ToString();
            bool result;
            string msg = $"【{palletCode}】还原任务失败";
            result = await _workTaskInfoApp.BackSetTaskAsync(id);
            if (result)
            {
                msg = $"【{palletCode}】还原任务成功";
                await AddTracesInfo(2, 0, $"托盘【{palletCode}】还原任务【{wmsTaskCode}】成功");
            }
            return CreateJsonGetResult(result, msg);
        }

        public ActionResult TaskRouteDetailInfoAsync(long id)
        {
            ViewBag.Id = id;
            //await AddTracesInfo();
            return View();
        }


    }
}
